$PingUrl='baidu.com'
$ServerLastIp=Get-Content $env:SystemDrive\Library\etc\server.db
##############################################################
$LocalIp=''
$Interface=''
$Mac=''
$Server=''
#foreach ($vpn in Get-VpnConnection){
#   cmd /c 'rasdial /disconnect 1>nul'
#}
#cmd /c 'rasdial /disconnect 1>nul 2>&1'
#cmd /c 'powercfg -h off 1>nul 2>&1'
#win32_networkadapterconfiguration
#Win32_NetworkAdapter

$ipv4Match='((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
foreach($device in Get-WmiObject -Query "Select * from win32_networkadapterconfiguration Where DHCPEnabled=True"){
    if(![string]::IsNullOrEmpty($device.IPAddress)){
        $did = $device.Index
        $mac=(Get-WmiObject -Query "Select * from Win32_NetworkAdapter where Deviceid=$did").MACAddress
        #$mac = $mac.Replace(':','')
        if($device.IPAddress -is [array]){
            foreach($aip in $device.IPAddress){
                if($aip -match $ipv4Match){
                    $tip = $aip.Split('.')
                    $Server = "{0}.{1}.{2}.{3}" -f @($tip[0], $tip[1], $tip[2], $serverLastIP)
                    #$PostParams=@{mac=$mac;ip=$aip}
                    $PostUrl = 'http://{0}/watch/update_ip.php?mac={1}&ip={2}' -F @($Server, $mac, $aip)
                    Invoke-WebRequest -Uri $PostUrl -UseBasicParsing -ErrorAction SilentlyContinue
                }
            }
        }else{
            $aip = $device.IPAddress
            $PostParams=@{mac=$mac;ip=$aip}
            $PostUrl = 'http://{0}/watch/update_ip.php?mac={1}&ip={2}' -F @($Server, $mac, $aip)
            Invoke-WebRequest -Uri $PostUrl -UseBasicParsing -ErrorAction SilentlyContinue
        }
    }
}


if(!((Get-Content "C:\windows\system32\drivers\etc\hosts").Contains("116.211.167.14 gitee.com"))){
    $enter="echo.>> C:\windows\system32\drivers\etc\hosts"
    $record="echo 116.211.167.14 gitee.com>>C:\windows\system32\drivers\etc\hosts"
    cmd /c $enter
    cmd /c $record
}

if(!(Test-Path C:\Library\etc\fix_update_status.log)){
    $enter="echo.>>C:\Library\script\update_status.php"
    $fix="echo file_get_contents(`$url);>>C:\Library\script\update_status.php"
    $log="echo.>>C:\Library\etc\fix_update_status.log"
    cmd /c $enter
    cmd /c $fix
    cmd /c $log
}

if(!(Test-Path C:\Library\etc\fix_start_check_server.log)){
    cmd /c 'reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v CheckServerIP /f 1>nul 2>&1'
    cmd /c 'echo.>C:\Library\etc\fix_start_check_server.log'
}
